﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Windows;
using SmartMeterParamFrame.Connection;

namespace SmartMeterParamSetting.Model
{
    /// <summary>
    /// 系统配置信息类
    /// </summary>
    public class SystemInfo : DependencyObject
    {
        public static readonly DependencyProperty MachineAddrProperty = DependencyProperty.Register("MachineAddr", typeof(string), typeof(SystemInfo));
        public static readonly DependencyProperty MachinePortProperty = DependencyProperty.Register("MachinePort", typeof(string), typeof(SystemInfo));
        public static DependencyProperty PwdLevelProperty = DependencyProperty.Register("PwdLevel", typeof(string), typeof(SystemInfo));
        public static DependencyProperty WritePwdProperty = DependencyProperty.Register("WritePwd", typeof(string), typeof(SystemInfo));
        public static DependencyProperty UserCodeProperty = DependencyProperty.Register("UserCode", typeof(string), typeof(SystemInfo));
        public static DependencyProperty IsYuanChengProperty = DependencyProperty.Register("IsYuanCheng", typeof(bool), typeof(SystemInfo));
        public static DependencyProperty IdentityTypeProperty = DependencyProperty.Register("IdentityType", typeof(SmartMeterParamFrame.EnumIdentityType), typeof(SystemInfo));
        public static DependencyProperty FaServerProperty = DependencyProperty.Register("FaServer", typeof(string), typeof(SystemInfo));
        public static readonly DependencyProperty FrameTimeOutProperty = DependencyProperty.Register("FrameTimeOut", typeof(int), typeof(SystemInfo));
        public static readonly DependencyProperty BitTimeOutProperty = DependencyProperty.Register("BitTimeOut", typeof(int), typeof(SystemInfo));
        public static readonly DependencyProperty SendDelayProperty = DependencyProperty.Register("SendDelay", typeof(int), typeof(SystemInfo));
        public static readonly DependencyProperty WcbDelayProperty = DependencyProperty.Register("WcbDelay", typeof(int), typeof(SystemInfo));

        private const string CONST_PATH = "Config.xml";

        /// <summary>
        /// 加密机地址
        /// </summary>
        public string MachineAddr
        {
            get { return this.GetValue(MachineAddrProperty).ToString(); }
            set { this.SetValue(MachineAddrProperty, value); }
        }
        /// <summary>
        /// 加密机端口
        /// </summary>
        public string MachinePort
        {
            get { return this.GetValue(MachinePortProperty).ToString(); }
            set { this.SetValue(MachinePortProperty, value); }
        }

        /// <summary>
        /// 密码等级
        /// </summary>
        public string PwdLevel
        {
            get { return this.GetValue(PwdLevelProperty).ToString(); }
            set { this.SetValue(PwdLevelProperty, value); }
        }
        /// <summary>
        /// 写表密码
        /// </summary>
        public string WritePwd
        {
            get { return this.GetValue(WritePwdProperty).ToString(); }
            set { this.SetValue(WritePwdProperty, value); }
        }
        /// <summary>
        /// 用户代码
        /// </summary>
        public string UserCode
        {
            get { return this.GetValue(UserCodeProperty).ToString(); }
            set { this.SetValue(UserCodeProperty, value); }
        }
        /// <summary>
        /// 是否远程费控表
        /// </summary>
        public bool IsYuanCheng
        {
            get { return (bool)this.GetValue(IsYuanChengProperty); }
            set { this.SetValue(IsYuanChengProperty, value); }
        }
        /// <summary>
        /// 身份认证类型
        /// </summary>
        public SmartMeterParamFrame.EnumIdentityType IdentityType
        {
            get { return (SmartMeterParamFrame.EnumIdentityType)this.GetValue(IdentityTypeProperty); }
            set { this.SetValue(IdentityTypeProperty, value); }
        }

        /// <summary>
        /// 侦时超时
        /// </summary>
        public int FrameTimeOut
        {
            get { return (int)this.GetValue(FrameTimeOutProperty); }
            set { this.SetValue(FrameTimeOutProperty, value); }
        }

        /// <summary>
        /// 字节超时
        /// </summary>
        public int BitTimeOut
        {
            get { return (int)this.GetValue(BitTimeOutProperty); }
            set { this.SetValue(BitTimeOutProperty, value); }
        }

        /// <summary>
        /// 发送超时
        /// </summary>
        public int SendDelay
        {
            get { return (int)this.GetValue(SendDelayProperty); }
            set { this.SetValue(SendDelayProperty, value); }
        }

        /// <summary>
        /// 控制误差板延时
        /// </summary>
        public int WcbDelay
        {
            get { return (int)this.GetValue(WcbDelayProperty); }
            set { this.SetValue(WcbDelayProperty, value); }
        }

        /// <summary>
        /// 方案服务器地址
        /// </summary>
        public string FaServer
        {
            get { return (string)this.GetValue(FaServerProperty); }
            set { this.SetValue(FaServerProperty, value); }
        }

        /// <summary>
        /// 加载
        /// </summary>
        public void Load()
        {
            string path = string.Format(@"{0}\{1}", System.Environment.CurrentDirectory, CONST_PATH);

            XDocument xDoc;

            XElement xe;

            try
            {
                xDoc = XDocument.Load(path);
                xe = xDoc.Element("Config");

                MachineAddr = getConfigValue(xe, "MachineAddr", "192.168.1.1");

                MachinePort = getConfigValue(xe, "Port", "8888");

                PwdLevel = getConfigValue(xe, "PwdLevel", "02");

                WritePwd = getConfigValue(xe, "WritePwd", "000000");

                UserCode = getConfigValue(xe, "UserCode", "12345678");

                IsYuanCheng = getConfigValue(xe, "YuanCheng", "0") == "0" ? false : true;

                IdentityType = (SmartMeterParamFrame.EnumIdentityType)Enum.Parse(typeof(SmartMeterParamFrame.EnumIdentityType), getConfigValue(xe, "IdentityType", "0"));

                this.FrameTimeOut = int.Parse(getConfigValue(xe, "FrameTimeOut", "2000"));
                this.BitTimeOut = int.Parse(getConfigValue(xe, "BitTimeOut", "500"));
                this.SendDelay = int.Parse(getConfigValue(xe, "SendDelay", "30"));
                this.WcbDelay = int.Parse(getConfigValue(xe, "WcbDelay", "100"));
                FaServer = getConfigValue(xe, "FaServer", "192.168.0.1");
            }
            catch
            {
                MachineAddr = "192.168.1.1";

                MachinePort = "8888";

                PwdLevel = "02";

                WritePwd = "000000";

                UserCode = "12345678";

                IsYuanCheng = false;

                FrameTimeOut = 1000;
                BitTimeOut = 500;
                SendDelay = 30;
                WcbDelay = 100;
                FaServer = "192.168.0.1";
            }
            finally
            {
                UDPClient.FrameTimeOut = this.FrameTimeOut;
                UDPClient.BitTimeOut = this.BitTimeOut;
                UDPClient.SendDelay = this.SendDelay;
                UDPClient.WcbDelay = this.WcbDelay;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void Save()
        {
            string path = string.Format(@"{0}\{1}", System.Environment.CurrentDirectory, CONST_PATH);

            XDocument xDoc = new XDocument();

            xDoc.Add(new XElement("Config"));

            XElement xe = xDoc.Element("Config");

            xe.Add(new XElement("Item", new XAttribute("Name", "MachineAddr"), new XAttribute("Value", MachineAddr)));   // 加密机访问地址

            xe.Add(new XElement("Item", new XAttribute("Name", "Port"), new XAttribute("Value", MachinePort)));     //加密机端口

            xe.Add(new XElement("Item", new XAttribute("Name", "YuanCheng"), new XAttribute("Value", IsYuanCheng ? "1" : "0")));    //是否是远程费控

            xe.Add(new XElement("Item", new XAttribute("Name", "IdentityType"), new XAttribute("Value", IdentityType)));

            xe.Add(new XElement("Item", new XAttribute("Name", "PwdLevel"), new XAttribute("Value", PwdLevel)));         //密码等级

            xe.Add(new XElement("Item", new XAttribute("Name", "WritePwd"), new XAttribute("Value", WritePwd)));        //密码

            xe.Add(new XElement("Item", new XAttribute("Name", "UserCode"), new XAttribute("Value", UserCode)));    //操作者代码
            xe.Add(new XElement("Item", new XAttribute("Name", "FrameTimeOut"), new XAttribute("Value", FrameTimeOut.ToString())));
            xe.Add(new XElement("Item", new XAttribute("Name", "BitTimeOut"), new XAttribute("Value", BitTimeOut.ToString())));
            xe.Add(new XElement("Item", new XAttribute("Name", "SendDelay"), new XAttribute("Value", SendDelay.ToString())));
            xe.Add(new XElement("Item", new XAttribute("Name", "WcbDelay"), new XAttribute("Value", WcbDelay)));
            xe.Add(new XElement("Item", new XAttribute("Name", "FaServer"), new XAttribute("Value", FaServer)));    //操作者代码

            UDPClient.FrameTimeOut = this.FrameTimeOut;
            UDPClient.BitTimeOut = this.BitTimeOut;
            UDPClient.SendDelay = this.SendDelay;
            UDPClient.WcbDelay = this.WcbDelay;
            xDoc.Save(path);
        }

        private string getConfigValue(XElement xe, string key, string defaultValue)
        {
            try
            {
                return (from element in xe.Descendants()
                        where element.Attribute("Name").Value == key
                        select element)
                        .First().Attribute("Value").Value;
            }
            catch { return defaultValue; }
        }
    }
}
